package com.kun.index.controller;


import cn.hutool.core.bean.BeanUtil;
import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.common.annotation.PermissionAuthentication;
import com.kun.index.controller.dto.LoginDto;
import com.kun.index.controller.dto.UserQueryDto;
import com.kun.index.entity.User;
import com.kun.index.service.impl.UserServiceImpl;
import com.kun.utils.CaptchaUtils;
import com.kun.utils.TokenUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-02-27
 */
@RestController
@RequestMapping("/index/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private CaptchaUtils captchaUtils;

    @PostMapping("/login")
    @ApiOperation("登录后台界面")
    @PermissionAuthentication
    public ResultDto login(@RequestBody LoginDto user){
        if ( user.getCaptchaInputCode()!=null && captchaUtils.verify(user.getCaptchaInputCode())){
            return userService.login(user);
        }else {
            return ResultDto.fail("验证码错误,请重新登录");
        }

    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    @PermissionAuthentication
    public ResultDto register(@RequestBody User user){
        if (user.getUsername()!=null){
            User user1= userService.queryUserByUsername(user.getUsername());
            if (user1==null){
                int num = userService.addUser(user);
                if (num>0){
                    return ResultDto.success("创建用户成功");
                }else {
                    return ResultDto.fail("创建用户失败");
                }
            }else {
                return ResultDto.fail("已经存在同名用户");
            }
        }else {
            return ResultDto.fail("用户名不能为空");
        }

    }

    @PostMapping("/saveUserInfo")
    @ApiOperation("保存用户信息")
    public ResultDto saveUserInfo(@RequestBody User user){

        if (userService.saveUser(user)>0){
            User currentUser = userService.queryUserById(user.getId());
            return ResultDto.success(currentUser);
        }else{
            return ResultDto.fail(Constants.CODE_302,"用户不存在");
        }
    }

    @GetMapping("/saveUserPassword")
    @ApiOperation("修改密码")
    public ResultDto saveUserPassword(@RequestParam("id") Integer id,
                                      @RequestParam("oldPassword") String oldPassword,
                                      @RequestParam("newPassword") String newPassword){
        if (userService.updatePassword(id,oldPassword,newPassword)){
            return ResultDto.success(TokenUtils.generateToken(""+id,newPassword));
        }else {
            return ResultDto.fail("原密码错误");
        }

    }

    @GetMapping("/queryAllUsers")
    @ApiOperation("查询所有的用户信息")
    public ResultDto queryAllUsers(){
        List<User> users = userService.queryAllUsers();
        List<UserQueryDto> list=new ArrayList<>();
        for (User user : users) {
            UserQueryDto userQueryDto = new UserQueryDto();
            BeanUtil.copyProperties(user,userQueryDto);
            list.add(userQueryDto);
        }
        if (list.size()>0){
            return ResultDto.success(list);
        }else{
            return ResultDto.fail("查询用户表错误");
        }
    }

    @GetMapping("/generateCaptcha")
    @ApiOperation("生成验证码")
    @PermissionAuthentication
    public ResultDto generateCaptcha(){
        String url = captchaUtils.createCode();
        return ResultDto.success(url);
    }






}

